import {
    getDict
} from "../../requestApi/login";

const dict = {
    state: {
        DICT: {
            drugCountUnitList: [],
            OnceDosageList: [],
            DrugFrequencyList: [],
            DrugDurationList: [],
            drugMethodList: []
        }
    },

    mutations: {
        SET_DICT: (state, options) => {
            state.DICT = options;
        }
    },

    actions: {
        GET_DICT({ commit }, data) {
            return new Promise((resolve, reject) => {
                getDict({}).then(response => {
                    if (response.code == 200 && response.data && response.data.arr) {

                        let datas = response.data.arr
                        let dict = {
                            drugCountUnitList: [],
                            OnceDosageList: [],
                            DrugFrequencyList: [],
                            DrugDurationList: []
                        }
                        if (datas) {
                            datas.forEach(e => {
                                if (e.title == '剂量单位') {
                                    dict.drugCountUnitList = e.contents
                                }
                                if (e.title == '单次剂量') {
                                    dict.OnceDosageList = e.contents
                                }
                                if (e.title == '用药频次') {
                                    dict.DrugFrequencyList = e.contents
                                }
                                if (e.title == '用药时长') {
                                    dict.DrugDurationList = e.contents
                                }
                                if (e.title == '用药方式') {
                                    dict.drugMethodList = e.contents
                                }
                            })
                            if (dict.OnceDosageList.length) {
                                dict.OnceDosageList.forEach(e => {
                                    e['value'] = e.content
                                })
                            }
                            if (dict.DrugFrequencyList.length) {
                                dict.DrugFrequencyList.forEach(e => {
                                    e['value'] = e.content
                                })
                            }
                            if (dict.DrugDurationList.length) {
                                dict.DrugDurationList.forEach(e => {
                                    e['value'] = e.content
                                })
                            }
                            if (dict.drugMethodList.length) {
                                dict.drugMethodList.forEach(e => {
                                    e['value'] = e.content
                                })
                            }
                            if (dict.drugCountUnitList.length) {
                                dict.drugCountUnitList.forEach(e => {
                                    e['value'] = e.content
                                })
                            }
                            commit("SET_DICT", dict);
                        }
                        resolve(response)
                    } else {
                        reject('error')
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
};

export default dict;